package org.proxywars.engine.rts.tests;

import org.proxywars.engine.rts.IUnit;
import org.proxywars.engine.rts.commands.AbstractCommand;

public abstract class UnitCreationCommand extends AbstractCommand {

	protected final int steps;
	protected int progress;

	public UnitCreationCommand(int steps) {
		this.steps = steps;
		progress = 0;
	}

	protected abstract IUnit createUnit();
	
	@Override
	public void process() {
		if (progress < steps)
			progress++;
		if (getCompletion() < 1.0) {
			fireProgressEvent();
		} else
			create();
	}

	private void create() {
		getOwner().addCreatedUnit(createUnit());
	}

	protected void fireProgressEvent() {
		System.out.format("%3.2f%%\r",
				getCompletion() * 100);
	}

	@Override
	public float getCompletion() {
		return (float) progress / (float) steps;
	}

}